
// 自定义模块
layui.config({
    base: 'plugins/ext/',   // 模块目录
    version: 'v1.5.11'
}).extend({             // 模块别名
    soulTable: 'soulTable',

});

layui.use(['form', 'table', 'soulTable'], function () {
    var table = layui.table,
        soulTable = layui.soulTable,
        form = layui.form;


    table.render({
        elem: '#myTable',
        method: "post",
        id: 'myTable',
        url: 'webapi/ich/list',
        even: true,
        height: 575,
        toolbar: '<div><button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</button></div>',
        page: true,
        overflow: 'tips',
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'id', title: '编号', width: 100, sort: true, filter: true},
            {field: 'name', title: '非遗名称', width: 150, sort: true, filter: true},
            {field: 'category', title: '所在类别', width: 165, filter: true},
            {field: 'nation', title: '所属民族', width: 123, filter: {split: '、'}},
            {field: 'location', title: '所属地区', width: 123, filter: true},
            {field: 'inheritors', title: '当前传承人', width: 150, filter: {split: '、'}, sort: true},
            {field: 'level', title: '非遗级别', width: 112, filter: true, sort: true,},
            {field: 'appliances', title: '涉及器具', width: 165, filter: {split: '、'}},
            {field: 'description', title: '具体描述', width: 550},
            {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'},
        ]],
        done: function () {
            soulTable.render(this)
        }
    });

    table.on('toolbar(myTable)', function (obj) {
        if (obj.event === 'add') {
            //增加新列
            layer.open({
                area: ['600px', '650px'],
                type: 2,
                title: "新增遗产表单",
                content: "/ich/add"
            })
        }
    });

    table.on('tool(myTable)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么？该操作不可撤销', function (index) {
                var id = data.id;
                $.ajax({
                    url: "/webapi/ich/delete/" + id,
                    method: "delete",
                    success: function (rs) {
                        layer.msg("删除成功！", {
                            time: 1000,
                        });
                        var page = $(".layui-laypage-skip .layui-input").val();
                        if (table.cache['myTable'].length === 1 && page > 1) {
                            table.reload('myTable', {
                                url: "/webapi/ich/list",
                                page: {
                                    curr: page - 1
                                }
                            })
                        } else {
                            table.reload('myTable', {
                                url: "/webapi/ich/list",
                            })
                        }
                    }
                })
                // table.reload('myTable', {
                //     url: 'webapi/ich/list'
                // });
                layer.close(index);
                return false;
            });
        } else if (obj.event === 'edit') {
            // layer.alert('编辑行：<br>'+ JSON.stringify(data))
            //编辑
            var id = data.id;
            layer.open({
                area: ['600px', '650px'],
                type: 2,
                title: "编辑遗产表单",
                content: "/ich/edit/" + id,
            })
        }
    });

    form.on('submit(search)', function (data) {
        table.reload('myTable', {
            where: data.field
        })
    })
})
